package com.tbmob.tb_h5.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.lang.reflect.Method;

//PhoneUtils.getPhoneImei(this);
public class PhoneUtils {

    private PhoneUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 获取IMEI码
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
     *
     * @return IMEI码
     */
    @SuppressLint("HardwareIds")
    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm != null ? tm.getDeviceId() : null;
    }

    public static String getPhoneImei(Context context) {
        String imei = PhoneUtils.getIMEI(context, 0);//取第一个imei
        if (TextUtils.isEmpty(imei)) {
            imei = PhoneUtils.getIMEI(context, 1);//第一个为空的话 取第二个
        }
        if (!TextUtils.isEmpty(imei) && imei.length() == 14) {
            imei = getIMEI14To15(imei);
        }
        if (TextUtils.isEmpty(imei)) {
            imei = PhoneUtils.getIMEI(context);//
        }
        return imei;
    }

    /**
     * Luhn算法 算出来第15位
     *
     * @param imei
     * @return
     */
    public static String getIMEI14To15(String imei) {
        int result = 0, i = 0, temp;
        while (i < imei.length()) {
            // 加上奇数位
            result += Integer.parseInt(imei.substring(i, ++i));
            // 偶数位乘以2
            temp = Integer.parseInt(imei.substring(i, ++i)) * 2;
            // 加上偶数位的十位和个位，这里做了优化操作
            // 偶数位乘2的乘积范围为[0, 18]，
            // 小于10直接加，大于10则个位与十位的和等于乘积减9
            result += temp < 10 ? temp : temp - 9;
        }
        // 取最终和的个位
        result %= 10;
        // 若为0则为0，否则为10减去个位
        result = result == 0 ? 0 : 10 - result;
        return imei + result;
    }

    @TargetApi(Build.VERSION_CODES.O)
    public static String getIMEI(Context context, int slotId) {
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                return getIMEI(slotId);
            } else {
                TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                return manager.getImei(slotId);
            }
        } catch (Throwable e) {
            return "";
        }
    }

    private static String getIMEI(int slotId) {
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);
            String imei = (String) method.invoke(null, "ril.gsm.imei", "");
            if (!TextUtils.isEmpty(imei)) {
                String[] split = imei.split(",");
                if (split.length > slotId) {
                    imei = split[slotId];
                }

                return imei;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return "";
    }

}